import 'package:flutter/material.dart';
import 'package:flutter_common_app/api/login_api.dart';
import 'package:flutter_common_app/data/user_info_data.dart';
import 'package:flutter_common_app/router/routes.dart';
import 'package:flutter_common_app/utils/common_util/toast.dart';
import 'package:flutter_common_app/utils/user_util.dart';
import 'package:flutter_common_app/widgets/empty_view/v_empty_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _pwdController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        brightness: Brightness.light,
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.only(
            left: 80.w,
            right: 80.w,
            top: 30.w,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Image.asset(
                'images/icon_logo.png',
                width: 120.w,
                height: 120.w,
              ),
              Container(
                margin: EdgeInsets.only(top: 30.w),
                child: Text(
                  'Welcome Back!',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: Colors.black87,
                    fontSize: 34.sp,
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 3.w),
                child: Text(
                  'Flutter 音乐',
                  style: TextStyle(
                    color: Colors.grey,
                    fontSize: 28.sp,
                  ),
                ),
              ),
              VEmptyView(50.w),
              TextField(
                controller: _phoneController,
                keyboardType: TextInputType.phone,
                decoration: InputDecoration(
                  hintText: '手机号',
                  hintStyle: TextStyle(
                    fontSize: 26.sp,
                  ),
                  prefixIcon: Icon(
                    Icons.phone_iphone,
                    color: Colors.grey,
                  ),
                ),
              ),
              VEmptyView(40.w),
              TextField(
                obscureText: true,
                controller: _pwdController,
                decoration: InputDecoration(
                  hintText: '密码',
                  hintStyle: TextStyle(
                    fontSize: 26.sp,
                  ),
                  prefixIcon: Icon(
                    Icons.lock,
                    color: Colors.grey,
                  ),
                ),
              ),
              VEmptyView(100.w),
              GestureDetector(
                onTap: () {
                  login();
                },
                behavior: HitTestBehavior.opaque,
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.red,
                    borderRadius: BorderRadius.all(
                      Radius.circular(10000.w),
                    ),
                  ),
                  height: 80.w,
                  width: 750.w,
                  alignment: Alignment.center,
                  child: Text(
                    '登录',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 34.sp,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  login() {
    var phone = _phoneController.text.trim();
    var password = _pwdController.text.trim();
    if (phone.isEmpty) {
      Toast.show('手机号不能为空');
      return;
    }
    if (password.isEmpty) {
      Toast.show('密码不能为空');
      return;
    }

    LoginApi(context)
      ..getParams()['phone'] = phone
      ..getParams()['password'] = password
      ..send().then((res) {
        if (res.data['code'] == 200) {
          UserUtil.updateUserModel(context,UserInfoData.fromJson(res.data));
          UserUtil.setSpUserInfo(res.data);
          Toast.show('登录成功');
          Routes.pop(context);
        }else{
          Toast.show(res.data['message']);
        }
      });
  }
}
